import { Component, OnInit, Input, SimpleChange } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
import { PagedQueryResult, Utils, BaseComponent } from 'iisp-ng';


import { DataTable, DataTableColumn,TableDataVO, TablePagedQueryParam } from '../dm.model.js';
import { DataManagerService } from '../dm.service.js';

declare var contextPath: string, tableCode: string;

@Component({
	selector: 'table-datas',
	templateUrl: contextPath + "/templates/iisp-data-mgr/tableDatas.tpl.ftl.htm",
	providers: [DataManagerService]
})
export class TableDataComponent extends BaseComponent implements OnInit {
    @Input() tableCode: string;
    dataTable: DataTable;
    filters: DataTableColumn[];
    dataResult: PagedQueryResult<any>;
    queryParam = new TablePagedQueryParam();

    constructor(private service: DataManagerService, private sanitizer: DomSanitizer) { super(); }

	ngOnInit(): void {
	    if (!this.tableCode) {
	        this.tableCode = tableCode;
	    }
	    //this.queryParam.pageSize = 5;
	    this.getDataTable();
	    this.getDataTableDatas(1);
	}

    ngOnChanges(changes: SimpleChange) {
        console.debug(JSON.stringify(changes));
        if(changes.currentValue != changes.previousValue) {
            this.tableCode = changes.currentValue;
            this.getDataTable();
            this.getDataTableDatas(1);
        }
    }
    
    getDataTable(): void {
        if(this.tableCode){
            this.service.getDataTable(this.tableCode).then(dataTable => {
                this.dataTable = dataTable;
                this.filters = new Array();
                for(let col of this.dataTable.columns){
                    if(col.options){
                        let obj =new Object();
                        for(let opt of col.options){
                            obj[opt.code]=opt.name;
                        }
                        col.options=obj;
                    }
                    if (col.isFilter){
                        this.filters.push(col);
                    }
                }
            }).catch(this.handleError);
        }
    }
	getDataTableDatas(event: any): void {
	    if (event){
	        this.queryParam.pageNumber = event;
	    }
	    if(this.tableCode){
	        this.service.getDataTableData(this.tableCode, this.queryParam).then(rst => this.dataResult = rst).catch(this.handleError);
	    }
	}
	format(data: any, col: DataTableColumn) {
	    if (data && col.pattern){
    	    if (col.dataType == 'number') {
    	        return Utils.formatNumber(data, col.pattern);
    	    }
    	    if (col.dataType == 'date') {
                if (typeof(data) == "number") {
                    data = new Date(data);
                } else if (data.length == 28 && data.indexOf("T") == 10){//"2017-11-29T12:18:54.921+0000"
                    data = new Date(data);
                }
    	        return Utils.formatDate(data, col.pattern);
    	    }
    	    if (col.dataType == 'percent') {
    	        return Utils.formatNumber(data*100, col.pattern) + "%";
    	    }
	    }
	    return data;
	}
    isRunning(): boolean{
        return this.service.isRunning > 0 || this.service.isRunning > 0;
    }
	
}
